package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.domain.model.LoginUser;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.enums.UpDownShelvesEnum;
import com.member.common.utils.StringUtils;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizGoodsBrowse;
import com.member.shop.domain.SysBizJingang;
import com.member.shop.service.ISysBizJingangService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【金刚区】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-12-27
 **/
@RestController
@RequestMapping("/shop/jingang")
public class SysBizJingangController extends BaseController {
    @Resource
    private ISysBizJingangService sysBizJingangService;

    /**
     * 查询金刚区列表
     */
    @PreAuthorize("@ss.hasPermi('shop:jingang:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizJingang sysBizJingang) {
        startPage();
        setTenantId(sysBizJingang);
        List<SysBizJingang> list = sysBizJingangService.selectSysBizJingangList(sysBizJingang);
        return getDataTable(list);
    }

    private void setTenantId(SysBizJingang sysBizJingang) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizJingang.setTenantId(tenantId);
        }
    }

    /**
     * 导出金刚区列表
     */
    @PreAuthorize("@ss.hasPermi('shop:jingang:export')")
    @Log(title = "金刚区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizJingang sysBizJingang) {
        setTenantId(sysBizJingang);
        List<SysBizJingang> list = sysBizJingangService.selectSysBizJingangList(sysBizJingang);
        ExcelUtil<SysBizJingang> util = new ExcelUtil<SysBizJingang>(SysBizJingang.class);
        util.exportExcel(response, list, "金刚区数据");
    }

    /**
     * 获取金刚区详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:jingang:query')")
    @GetMapping(value = "/{jingangId}")
    public AjaxResult getInfo(@PathVariable("jingangId") Long jingangId) {
        return success(sysBizJingangService.selectSysBizJingangByJingangId(jingangId));
    }

    /**
     * 新增金刚区
     */
    @PreAuthorize("@ss.hasPermi('shop:jingang:add')")
    @Log(title = "金刚区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizJingang sysBizJingang) {
  
        if (sysBizJingang.getDisplayed().equals(UpDownShelvesEnum.GROUNDING.getKey()) && !sysBizJingangService.selectGroundingNum(sysBizJingang)) {
            return warn("金刚区上架最多20个");
        }

        if (StringUtils.isNotEmpty(sysBizJingang.getJingangName())) {
            LambdaQueryWrapper<SysBizJingang> queryWrapper = new LambdaQueryWrapper<SysBizJingang>();
            queryWrapper.eq(SysBizJingang::getTenantId, sysBizJingang.getTenantId());
            queryWrapper.eq(StringUtils.isNotEmpty(sysBizJingang.getJingangName()), SysBizJingang::getJingangName, sysBizJingang.getJingangName());
            queryWrapper.eq(SysBizJingang::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
            List<SysBizJingang> sysBizJingangs = sysBizJingangService.selectSysBizJingangList(queryWrapper);
            if (sysBizJingangs.size() > 0) {
                return warn("金刚区名称已存在");
            }
        }
        setTenantId(sysBizJingang);
        return toAjax(sysBizJingangService.insertSysBizJingang(sysBizJingang));
    }

    /**
     * 修改金刚区
     */
    @PreAuthorize("@ss.hasPermi('shop:jingang:edit')")
    @Log(title = "金刚区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizJingang sysBizJingang) {
        if (sysBizJingang.getDisplayed().equals(UpDownShelvesEnum.GROUNDING.getKey()) && !sysBizJingangService.selectGroundingNum(sysBizJingang)) {
            return warn("金刚区上架最多20个");
        }
        if (StringUtils.isNotEmpty(sysBizJingang.getJingangName())) {
            LambdaQueryWrapper<SysBizJingang> queryWrapper = new LambdaQueryWrapper<SysBizJingang>();
            queryWrapper.eq(StringUtils.isNotEmpty(sysBizJingang.getJingangName()), SysBizJingang::getJingangName, sysBizJingang.getJingangName());
            queryWrapper.ne(SysBizJingang::getJingangId, sysBizJingang.getJingangId());
            queryWrapper.eq(SysBizJingang::getTenantId, sysBizJingang.getTenantId());
            queryWrapper.eq(SysBizJingang::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
            List<SysBizJingang> sysBizJingangs = sysBizJingangService.selectSysBizJingangList(queryWrapper);
            if (!sysBizJingangs.isEmpty()) {
                return warn("金刚区名称已存在");
            }
        }
        return toAjax(sysBizJingangService.updateSysBizJingang(sysBizJingang));
    }

    /**
     * 删除金刚区
     */
    @PreAuthorize("@ss.hasPermi('shop:jingang:remove')")
    @Log(title = "金刚区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jingangId}")
    public AjaxResult remove(@PathVariable("jingangId") Long jingangId) {
        SysBizJingang sysBizJingang = new SysBizJingang();
        sysBizJingang.setDeleted(OrderDeleteEnum.DELETE.getKey());
        sysBizJingang.setJingangId(jingangId);
        return toAjax(sysBizJingangService.updateSysBizJingang(sysBizJingang));
    }
}
